package com.tiny_wind.tiny_wind_diary.model;

import java.util.List;

/**
 * 封装json对象，所有返回结果都使用它
 */
public class TwResultModel<T> {
	// 设为private的不会显示出来.
	// 返回代码 0为成功,其余为各种异常.
	public int code;
	// 成功时返回的数据
	public TwResultDataModel<T> resultData;
	// 错误信息
	public String message;


	public TwResultModel(int code, TwResultDataModel<T> resultData, String message) {
		this.code = code;
		this.message = message;
		this.resultData = resultData;
	}

	/**
	 * 生成一个成功的不含返回数据的实例.
	 */
	public TwResultModel() {
		this.code = 0;
		this.message = "操作成功";
		this.resultData = null;
	}

	// 成功时无需返回结果的构造器
	public TwResultModel(int code) {
		this.code = code;
		if (code == 0) {
			this.message = "操作成功";
		} else {
			this.message = "操作失败";
		}
		this.resultData = null;
//		this.itemCount = 0;
	}

	// 成功时的构造器
	public TwResultModel(int code, TwResultDataModel<T> resultData) {
		this.code = code;
		this.resultData = resultData;
		this.message = "操作成功";
//		this.itemCount = 0;
	}
	
	// 成功时的构造器
	public TwResultModel(TwResultDataModel<T> resultData) {
		this.code = 0;
		this.resultData = resultData;
		this.message = "操作成功";
//		this.itemCount = 0;
	}

	// 1 成功时的构造器
	// 1.1 成功时的构造器
	public TwResultModel(int code, TwResultDataModel<T> resultData, int itemCount) {
		this.code = code;
		this.resultData = resultData;
		this.message = "操作成功";
//		this.itemCount = itemCount;
	}

	// 1.2 返回单个item的构造器
	public TwResultModel(int code, T item) {
		this.code = code;
		this.resultData = new TwResultDataModel<T>(item);
		this.message = "操作成功";
//		this.itemCount = 1;
	}

	// 1.3 返回多个item的构造器
	public TwResultModel(int code, List<T> items, TwPageModel page) {
		this.code = code;
		this.resultData = new TwResultDataModel(items, page);
		this.message = "操作成功";
//		this.itemCount = items.size();
	}

	// 错误时的构造器,code + error
	public TwResultModel(int code, String error) {
		this.code = code;
		this.message = error;
		this.resultData = null;
	}
	//返回无数据返回结果.
//	public static Result errorResult(int code,String message) {
//		Result<> result = new Result(-1);
//		result.message = message;
//		return result;
//		
//	}
	public TwResultModel(T item) {
		TwResultDataModel resultData = new TwResultDataModel(item);
		this.code = 0;
		this.resultData = resultData;
		this.message = "操作成功";
	}
	public TwResultModel(T[] item) {
		TwResultDataModel resultData = new TwResultDataModel(item);
		this.code = 0;
		this.resultData = resultData;
		this.message = "操作成功";
	}

	public TwResultModel(TwErrorModel error) {
		this.code = error.code;
		this.message = error.message;
	}
}
